<?php
function noAdmin()
{
	return '404 Not Found.';
}

function getLastOrderEntry()
{
	$tables = array();
	$tables[0] = 'inv_orders';
	
	$fields = array();
	$fields[0] = '*';
	
	$whereClause = '';
	
	$data = SQLRecover($tables, $fields, $whereClause);
}

function listAccounts($type)
{
	$tables = array();
	$tables[0] = 'core_accounts a';
	$tables[1] = 'core_accounts_contacts c';
	
	$fields = array();
	$fields[0] = 'a.accountID id';
	$fields[1] = 'c.detail name';
	
	$whereClause = 'a.accountID = c.accountID AND ((a.accountType = "'.$type.'" OR a.accountType = "mixed") AND c.type = "_name") ORDER BY c.detail ASC';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	$output = '';
	
	if($data['queryStatus'] == 'DB Query success!')
	{
		for ($i=0; $i<sizeOf($data['id']); $i++)
		{
			$selected = ($i == 0 ? ' selected' : '');
			$output .= '<option value="'.$data['id'][$i].'"'.$selected.'>'.$data['name'][$i].'</option>';
		}
	}
	else
	{
		$output = '<option value="---">No Entries Avail.</option>';
	}
	return $output;
}

function salesPurchase()
{
	//YYYY-MM-DD HH:MM:SS <- DATE TIME FORMAT TAKE NOTE WHEN CODING B.LOGIC
	
	if(get('number') == 'null')
	{
		$hiderVar = ' hidden';
	}
	else if(get('number') == 'new')
	{
		$hiderVar = '';
	}
	else
	{
		$tables = array();
		$tables[0] = 'inv_orders';
		
		$fields = array();
		$fields[0] = 'orderID';
		
		$whereClause = 'orderID="'.get('number').'"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		if($data['queryStatus'] != 'DB Query success!')
		{
			$hiderVar = ' hidden';
		}
		else
		{
			$hiderVar = '';
		}
	}
	
	$client = (get('form') == 'Sales' ? 'Client' : 'Supplier');
	$SO = (get('form') == 'Sales' ? 'Check-Out' : 'Check-In');
	$options = listAccounts(strtolower($client));
	$hideNone = ($SO == 'Check-In' ? ' class="hidden"' : '');
	$checkFields = ($SO == 'Check-In' ? '
				<div class="grid_4 alpha itemC">
					<h3>Item Code</h3>&nbsp;&nbsp;[<a href="#" id="itemCodeHelp">?</a>]
				</div>
				<div class="grid_2">
					<h3>Serial No.</h3>
				</div>
				<div class="grid_2">
					<h3>Quantity</h3>
				</div>
				<div class="grid_2">
					<h3>Location</h3>
				</div>
				<div class="grid_2 omega">
					<h3>Comment</h3>
				</div>
	' : '
				<div class="grid_2 alpha">
					<h3>Item Code</h3>
				</div>
				<div class="grid_2">
					<h3>Search By</h3>
				</div>
				<div class="grid_2">
					<h3>Serial Number</h3>
				</div>
				<div class="grid_2">
					<h3>Quantity</h3>
				</div>
				<div class="grid_2">
					<h3>Price</h3>
				</div>
				<div class="grid_2 omega">
					<h3>Total</h3>
				</div>
	');
		$output = '<div id="orderPointer" class="grid_12 mainBox bordered" rel="'.$SO.'">
			<div class="grid_3 alpha">
				<div id="leftTop">
					<h1>'.$SO.'</h1>
					<h2>'.$SO.' form # </h2>
					<form class="inline" action="index.php" method="GET">
						<input type="hidden" name="page" value="orders" />
						<input type="hidden" name="form" value="'.get('form').'" />
						<input type="text" name="number"/><input type="submit" style="display: none;">
					</form>
					
					<input type="button" value="New '.$SO.' Record" class="buttonsSmall linkButton" alt="'.INDEX.'orders&form='.get('form').'&number=new"/>
				</div>
			</div>
			<div class="grid_9 omega">
				<div id="rightTop">
					<h3>Help</h3>
					<p>This module is called the '.strtolower($SO).' module. This enables you to log in your inventory activities and identify related materials to add/subtract from your current inventory count. If you experience any technical difficulties, do not hesitate to send us an inquiry through our website.</p>
					<p class="rightAlign">
						- '.logo().'
					</p>
				</div>
			</div>
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div id="orderMain" class="grid_12 mainBox'.$hiderVar.'">
			<div class="grid_4 alpha">
			<h3>'.$client.'</h3>
			<select id="clisup" name="clisup" size="6" class="globalSelector">
				'.$options.'
			</select>
			</div>
			<div class="grid_4">
			<h3>Date</h3>
			<input type="text" name="date" value="'.shortDateNow().' '.getTimeNow().'" id="dateBox" class="inputDate" />
			</div>
			<div class="grid_4 omega">
				<h3>State</h3>
				<p class="midRight">
					<input type="radio" class="radio" name="state" value="confirmed" checked/> Check '.str_ireplace('Check-', '', $SO).'<br />
					<input type="radio" class="radio" name="state" value="pending" /> Pending<br />
					<input type="radio" class="radio" name="state" value="delete" /> Cancel<br />
				</p>
				<div'.$hideNone.'>
					<h3>Grand Total</h3>
					<div id="grandTotal">
					</div>
				</div>
			</div>
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_12 mainBox bordered'.$hiderVar.' ">
			<div class="grid_12 alpha">
				<input type="button" value="Add Item" id="itemAdder" />
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			<div id="hiderDesc">
				'.$checkFields.'
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			
			<div id="itemConstructor">
			</div>
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_12 mainBox centerAlign'.$hiderVar.'">
			<input id="updater" type="submit" class="saveUpdate" value="Save" />
				<input type="button" class="saveUpdate" value="Cancel"/>
		</div>
		';
	return $output;
}
function saveP(){

	$sono = post('sono');
	$date = sqlDateTime();
	$state = post('state');
	$supp = post('clisup');
	$itemName = post('itemName');
	$quantity = post('quantity');
	$unitPrice = post('unitPrice');
	if($state == 'pending')
		$state = 1;
	elseif($state == 'delete')
		$state = 2;
	else
		$state = 3;
	
	$table = 'inv_orders';
	$fields = array();
	$fields[0] = 'timestamp';
	$fields[1] = 'createdBy';
	$fields[2] = 'orderNo';
	$fields[3] = 'type';
	$fields[4] = 'status';
	$fields[5] = 'lastTouch';
	
	$values = array();
	$values[0] = $date;
	$values[1] = 'a';
	$values[2] = $sono;
	$values[3] = 1;
	$values[4] = $state;
	$values[5] = 'b';
	
	$data2 = SQLInsert($table, $fields, $values);

	$table2 = 'inv_actions';
	$count = count($itemName);
	$fields2 = array();
	$fields2[0] = 'orderID';
	$fields2[1] = 'quantity';
	$fields2[2] = 'unitPrice';
	$fields2[3] = 'dateStamp';
	$fields2[4] = 'userID';
	$fields2[5] = 'itemID';
	$latestO = getLatest($table, 'orderID');
	$values[0] = $latestO['orderID'][0];
	$values[3] = $date;
	$values[4] = 1;
	$values[5] = 1;
	for($i = 0; $i < $count; $i++)
	{
		$values[1] = $quantity[$i];
		$values[2] = $unitPrice[$i];
		$data2 = SQLInsert($table2, $fields2, $values);
	}
		
	return $display;
}
function displayPage()
{
	$output = salesPurchase();
	$output = (get('form') == 'null' ? noAdmin() : $output);
	return adminContent($output, noAdmin());
}
?>